import Immutable from 'seamless-immutable';
import * as types from '../constants/ActionTypes';
import helper from '../helper/childrenDeviceHelper.js';

const childrenDevice = Immutable.from({
  childrenDeviceList: {},
  data_list: [],
  batchDataList: [],
  saveMsg: {}
});
export default function testCategoryReducer(state = childrenDevice, action) {
  const { type, payload } = action;
  switch (type) {
    case types.FETCH_CHILDREN_DEVICE:
      return state.setIn(['childrenDeviceList'], payload)
        .setIn(['data_list'], helper.addItemId(payload.data.data_list));
    case types.BATCH_ADD:
      return state.setIn(['data_list'], helper.connetBatch(state.data_list, payload.sns));
    case types.ADD_ONE:
      return state.setIn(['data_list'], helper.addOne(state.data_list));
    case types.EDIT_DEVICE:
      return state.setIn(['data_list'], payload);
    case types.RESTORE_CHILDREN_DEVICE:
      return state.setIn(['childrenDeviceList'], {});
    case types.SAVE_ERROR_MSG:
      return state.setIn(['saveMsg'], payload);
    default:
      return state;
  }
}
